package SlidingWindow;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Hua YY
 * Date: 2024-10-20
 * Time: 21:24
 */
class Solution5 {
    public int minOperations(int[] nums, int x) {
        int left = 0 , right = 0 , count = 0 ,sum = 0;
        int n = nums.length;
        for(int i = 0 ; i < n ; i++){
            sum += nums[i];
        }
        int target = sum - x , tem = 0;
        if(target < 0){
            return -1;
        }
        if(target == 0){
            return n;
        }
        for(; right < n ; right++){
            //进窗口不判断
            tem += nums[right];
            while(tem > target ){
                tem -= nums[left];
                left++;
            }//执行顺序也有讲究，最后一步判断
            if(tem == target){
                count = Math.max(count , right-left+1);
            }
        }
        if(count == 0){
            return -1;
        }
        return n-count;
    }
}
public class Test5 {
}
